//
//  TimeView.m
//  EasyToo
//
//  Created by hydb on 13-6-12.
//
//

#import "TimeView.h"


@implementation TimeView
{
    NSMutableArray *_airCompany;
    NSMutableArray *_startTime;
    NSMutableArray *_startPort;
    NSMutableArray *_arriveTime;
    NSMutableArray *_arrivePort;
    NSMutableArray *_discountPrice;
    NSMutableArray *_allPrice;
    
    UILabel *_descLabel;
    UILabel *_airCompanyLabel;
    UILabel *_startTimeLabel;
    UILabel *_startPortLabel;
    UILabel *_arriveTimeLabel;
    UILabel *_arrivePortLabel;
    UILabel *_discountPriceLabel;
    UILabel *_allPriceLabel;
}

- (void)dealloc
{
    [_airCompany release];
    [_startTime release];
    [_startPort release];
    [_arriveTime release];
    [_arrivePort release];
    [_discountPrice release];
    [_allPrice release];
    [_descLabel release];
    [_airCompanyLabel release];
    [_startTimeLabel release];
    [_startPortLabel release];
    [_arriveTimeLabel release];
    [_arrivePortLabel release];
    [_discountPriceLabel release];
    [_allPriceLabel release];
    
    _airCompany = nil;
    _startTime = nil;
    _startPort = nil;
    _arriveTime = nil;
    _arrivePort = nil;
    _discountPrice = nil;
    _allPrice = nil;
    _descLabel = nil;
    _airCompanyLabel = nil;
    _startTimeLabel = nil;
    _startPortLabel = nil;
    _arriveTimeLabel = nil;
    _arrivePortLabel = nil;
    _discountPriceLabel = nil;
    _allPriceLabel = nil;
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    _airCompany = [[NSMutableArray alloc]init];
    _startTime = [[NSMutableArray alloc]init];
    _startPort = [[NSMutableArray alloc]init];
    _arriveTime = [[NSMutableArray alloc]init];
    _arrivePort = [[NSMutableArray alloc]init];
    _discountPrice = [[NSMutableArray alloc]init];
    _allPrice = [[NSMutableArray alloc]init];

    self = [super initWithFrame:frame];
    if (self) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [self addSubview:tableView];
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        [tableView setFrame:CGRectMake(0, 0, width, height)];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        tableView.separatorColor = [UIColor underPageBackgroundColor];
        self.tableview = tableView;

    }
    return self;
}



//更新数据
- (void) getNewData
{
    [_airCompany addObject:@"春秋航空公司"];
    [_airCompany addObject:@"深圳航空公司"];
    [_airCompany addObject:@"海南航空公司"];
    [_airCompany addObject:@"南方航空公司"];
    
    [_startTime addObject:@"20：45"];
    [_startTime addObject:@"20：45"];
    [_startTime addObject:@"20：45"];
    [_startTime addObject:@"20：45"];

    [_startPort addObject:@"白云机场"];
    [_startPort addObject:@"白云机场"];
    [_startPort addObject:@"白云机场"];
    [_startPort addObject:@"白云机场"];

    [_arriveTime addObject:@"23：12"];
    [_arriveTime addObject:@"23：12"];
    [_arriveTime addObject:@"23：12"];
    [_arriveTime addObject:@"23：12"];
    
    [_arrivePort addObject:@"宝安机场"];
    [_arrivePort addObject:@"宝安机场"];
    [_arrivePort addObject:@"宝安机场"];
    [_arrivePort addObject:@"宝安机场"];
    
    [_discountPrice addObject:@"980"];
    [_discountPrice addObject:@"980"];
    [_discountPrice addObject:@"980"];
    [_discountPrice addObject:@"980"];
    
    [_allPrice addObject:@"1020"];
    [_allPrice addObject:@"1020"];
    [_allPrice addObject:@"1020"];
    [_allPrice addObject:@"1020"];

}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_airCompany count] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    // 列寬
    CGFloat contentWidth = self.frame.size.width;
    
    if (indexPath.row == 0) {
        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, contentWidth, 30)];
        _descLabel.text = [NSString stringWithFormat:@"%@  %@ -> %@", self.startDate, self.startCity, self.arriveCity];
        _descLabel.font = [UIFont systemFontOfSize:15];
        _descLabel.backgroundColor = [UIColor grayColor];
        _descLabel.textAlignment = UITextAlignmentCenter;
        [cell.contentView addSubview:_descLabel];
    } else {
        
        NSInteger row = [indexPath row] - 1;
        
        //航空公司
        _airCompanyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 25)];
        _airCompanyLabel.text = [_airCompany objectAtIndex:row];
        _airCompanyLabel.font = [UIFont systemFontOfSize:12];
        _airCompanyLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_airCompanyLabel];
        
        //起飞时间
        _startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 50, 20)];
        _startTimeLabel.text = [_startTime objectAtIndex:row];
        _startTimeLabel.font = [UIFont systemFontOfSize:12];
        _startTimeLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_startTimeLabel];
        
        //起飞机场
        _startPortLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 35, 70, 20)];
        _startPortLabel.text = [_startPort objectAtIndex:row];
        _startPortLabel.font = [UIFont systemFontOfSize:12];
        _startPortLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_startPortLabel];

        //到达时间
        _arriveTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 50, 20)];
        _arriveTimeLabel.text = [_arriveTime objectAtIndex:row];
        _arriveTimeLabel.font = [UIFont systemFontOfSize:12];
        _arriveTimeLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_arriveTimeLabel];
        
        //到达机场
        _arrivePortLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 55, 70, 20)];
        _arrivePortLabel.text = [_arrivePort objectAtIndex:row];
        _arrivePortLabel.font = [UIFont systemFontOfSize:12];
        _arrivePortLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_arrivePortLabel];
        
        //折扣价
        _discountPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 15, 150, 30)];
        _discountPriceLabel.text = [NSString stringWithFormat:@"￥%@", [_discountPrice objectAtIndex:row]];
        _discountPriceLabel.font = [UIFont systemFontOfSize:22];
        _discountPriceLabel.textColor = [UIColor redColor];
        _discountPriceLabel.textAlignment = UITextAlignmentRight;
        _discountPriceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_discountPriceLabel];
        
        //原价
        _allPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 150, 30)];
        _allPriceLabel.text = [NSString stringWithFormat:@"市场价：￥%@", [_allPrice objectAtIndex:row]];
        _allPriceLabel.font = [UIFont systemFontOfSize:12];
        _allPriceLabel.textColor = [UIColor grayColor];
        _allPriceLabel.textAlignment = UITextAlignmentRight;
        _allPriceLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:_allPriceLabel];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 30;
    } else {
        return 90;
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    cell.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:252.0/255.0 alpha:1.0];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    [self.delegate onClickTableItem:indexPath.row];
}

@end
